/**
 * file
 */
package com.tdkj.action.front.member.seller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.risevalue.BeanFloatColorCustom;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.ServiceException;
import com.tdkj.service.risevalue.IBeanFloatColorCustomService;
import com.tdkj.service.shop.IBeanGoodService;
import com.tdkj.util.PublicConfing;

public class GoodManagementAction extends BaseAction {

	
	private Long id;
	private Long typeId;
	private Long ticketcolorId;
	private Long userId; 
	private Long shelfId;
	
	private BeanGood good;
	
	private List<BeanGood> goods;
	
	private List<BeanFloatColorCustom> ticketcolors;
	
	private IBeanGoodService goodService;
	
	private IBeanFloatColorCustomService ticketColorCustomService;
	
	//库存 增减
	private Integer reduce ;
	
	private Integer add ;
	
	public Integer getReduce() {
		return reduce;
	}
	public void setReduce(Integer reduce) {
		this.reduce = reduce;
	}
	public Integer getAdd() {
		return add;
	}
	public void setAdd(Integer add) {
		this.add = add;
	}

	@Override
	public String execute() {
		ticketcolors = ticketColorCustomService.getAll();
		good = new BeanGood();
		return "success";
	}
	
	public String add() {
		//BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		//userId = user.getId();
		
		//初始化默认的参数 兴建添加  可修改
		good.setRateOfIncrease(new BigDecimal(0));
		good.setSellType(0);
		//初始化默认的参数 兴建添加  可修改 end
		
		good.setUser(this.getCurrentUser());
		good.setAuditState("0");
		good.setSubmitTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().save(good);
		//id = goodService.save(good).getId();
		//return edit();
		return goodList();
	}
	
	public String edit() {
		good = goodService.getGoodById(id);
		ticketcolors = ticketColorCustomService.getAll();
		return "edit";
	}
	
	public String update() {
		goodService.update(good);
		//id = good.getId();
		//return edit();
		return goodList();
	}
	
	public String delete() {
		goodService.delete(id, BeanGood.class);
		return goodList();
	}
	
	
	public String goodList() {
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		userId = user.getId();
		
		String name = this.getQueryInfo().get("name");
		
		String hql = " from " +BeanGood.class.getName() + " as bean where 1=1 ";
		hql = hql + " and bean.user.id='"+user.getId()+"' ";
		if(name!=null && !"".equals(name)){
			hql = hql + " and bean.name like '%"+name+"%' ";
		}
		hql = hql + " order by bean.submitTime";
		Query query = this.createQuery(hql);
		
		this.queryByPage(query, "goodManagementAction!goodList.action", 10);
		return "goodList";
	}
	
	public String publish() throws ServiceException {
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		userId = user.getId();
		shelfId = goodService.getDefaultShelfByUserId(userId).getId();
		goodService.publish(id, shelfId);
		return goodList();
	}
	
	public String unpublish() {
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		userId = user.getId();
		
		goodService.unpublish(id);
		return goodList();
	}
	
	
	
	public String recommend() throws ServiceException{
		goodService.createRecommend(id);
		return goodList();
	}
	
	public String reduceInventory() {
		good = goodService.getGoodById(id);
		return "reduceInventory";
	}
	
	public String addInventory() {
		good = goodService.getGoodById(id);
		return "addInventory";
	}
	
	public String reduceInventorySave() {
		goodService.updateInventory(good.getId(), -reduce);
		//id = good.getId();
		//return edit();
		return goodList();
	}
	
	public String addInventorySave() {
		goodService.updateInventory(good.getId(), add);
		//id = good.getId();
		//return edit();
		return goodList();
	}
	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the typeId
	 */
	public Long getTypeId() {
		return typeId;
	}

	/**
	 * @param typeId the typeId to set
	 */
	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	/**
	 * @return the ticketcolorId
	 */
	public Long getTicketcolorId() {
		return ticketcolorId;
	}

	/**
	 * @param ticketcolorId the ticketcolorId to set
	 */
	public void setTicketcolorId(Long ticketcolorId) {
		this.ticketcolorId = ticketcolorId;
	}

	/**
	 * @return the userId
	 */
	public Long getUserId() {
		return userId;
	}

	/**
	 * @param userId the userId to set
	 */
	public void setUserId(Long userId) {
		this.userId = userId;
	}

	/**
	 * @return the shelfId
	 */
	public Long getShelfId() {
		return shelfId;
	}

	/**
	 * @param shelfId the shelfId to set
	 */
	public void setShelfId(Long shelfId) {
		this.shelfId = shelfId;
	}

	/**
	 * @return the good
	 */
	public BeanGood getGood() {
		return good;
	}

	/**
	 * @param good the good to set
	 */
	public void setGood(BeanGood good) {
		this.good = good;
	}

	/**
	 * @param goodService the goodService to set
	 */
	@Resource
	public void setGoodService(IBeanGoodService goodService) {
		this.goodService = goodService;
	}

	/**
	 * @return the goods
	 */
	public List<BeanGood> getGoods() {
		return goods;
	}

	/**
	 * @param goods the goods to set
	 */
	public void setGoods(List<BeanGood> goods) {
		this.goods = goods;
	}

	/**
	 * @param ticketColorCustomService the ticketColorCustomService to set
	 */
	@Resource
	public void setTicketColorCustomService(
			IBeanFloatColorCustomService ticketColorCustomService) {
		this.ticketColorCustomService = ticketColorCustomService;
	}

	/**
	 * @return the ticketcolors
	 */
	public List<BeanFloatColorCustom> getTicketcolors() {
		return ticketcolors;
	}

	/**
	 * @param ticketcolors the ticketcolors to set
	 */
	public void setTicketcolors(List<BeanFloatColorCustom> ticketcolors) {
		this.ticketcolors = ticketcolors;
	}
	
	
}
